package com.example.demo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "MongoDb测试", tags = "MongoDb测试")
@RequestMapping("rest/mongo")
public class MongoDbController {

    @Autowired
    private MongoDbService mongoDbService;

    @ApiOperation(value = "添加书本", notes = "添加书本")
    @PostMapping("save")
    public String saveObj(@RequestBody Book book) {
        return mongoDbService.saveObj(book);
    }

    @ApiOperation(value = "查询全部", notes = "查询全部")
    @GetMapping("findAll")
    public List<Book> findAll() {
        return mongoDbService.findAll();
    }

    @ApiOperation(value = "查询某本书", notes = "查询某本书")
    @ApiImplicitParam(name = "id", value = "书本主键", required = true, dataType = "String")
    @GetMapping("findOne")
    public Book findOne(@RequestParam String id) {
        return mongoDbService.getBookById(id);
    }

    @ApiOperation(value = "根据书本名称查询", notes = "根据书本名称查询")
    @ApiImplicitParam(name = "name", value = "书本名称", required = true, dataType = "String")
    @GetMapping("findOneByName")
    public Book findOneByName(@RequestParam String name) {
        return mongoDbService.getBookByName(name);
    }

    @ApiOperation(value = "编辑书本", notes = "编辑书本")
    @PostMapping("update")
    public String update(@RequestBody Book book) {
        return mongoDbService.updateBook(book);
    }

    @ApiOperation(value = "删除书本", notes = "删除书本")
    @PostMapping("delOne")
    public String delOne(@RequestBody Book book) {
        return mongoDbService.deleteBook(book);
    }

    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    @ApiImplicitParam(name = "id", value = "书本主键", required = true, dataType = "String")
    @GetMapping("delById")
    public String delById(@RequestParam String id) {
        return mongoDbService.deleteBookById(id);
    }

    @ApiOperation(value = "模糊查询", notes = "模糊查询")
    @ApiImplicitParam(name = "search", value = "条件", required = true, dataType = "String")
    @GetMapping("findlikes")
    public List<Book> findByLikes(@RequestParam String search) {
        return mongoDbService.findByLikes(search);
    }
}